package com.xiao.bokeyuan.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class BitmapUtils {
	public static Bitmap getBitmap(byte[] data,int width,int height){
		Options options=new Options();
		options.inJustDecodeBounds=false;
		BitmapFactory.decodeByteArray(data, 0, data.length,options);
		int xScale=options.outWidth/width;
		int yScale=options.outHeight/height;
		if(xScale>yScale){
			options.inSampleSize=xScale;
		}else{
			options.inSampleSize=yScale;
		}
		Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length,options);
		return bitmap;
	}
	
	public static Bitmap  getBitmap(String path) throws FileNotFoundException{
		FileInputStream in=new FileInputStream(path);
		return BitmapFactory.decodeStream(in);
	}
	public static void saveBitmap(Bitmap bitmap,String path) throws FileNotFoundException{
		File file=new File(path);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		FileOutputStream out = new FileOutputStream(path);
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
	}
}
